package cn.fl.ref.fnccappayeeinfobig.util;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

import net.sf.json.JSONArray;
import net.sf.json.JSONException;
import net.sf.json.JsonConfig;
import net.sf.json.processors.JsonValueProcessor;
import net.sf.json.util.PropertySetStrategy;
import cn.cmm.base.framework.utils.StringUtils;

public class JsonUtil {

	public static List<?> toList(String jsons, Object obj) {

		if (StringUtils.isBlank(jsons))
			return null;

		JsonConfig config = new JsonConfig();
		config.setClassMap(new HashMap<String, Object>());
		config.setPropertySetStrategy(new PropertyStrategyWrapper(
				PropertySetStrategy.DEFAULT));
		config.registerJsonValueProcessor(Date.class , new JsonDateValueProcessor());  
		
		JSONArray jsonArray = JSONArray.fromObject(jsons);
		return JSONArray.toList(jsonArray, obj, config);

	}
}

class PropertyStrategyWrapper extends PropertySetStrategy {
	private PropertySetStrategy original;

	public PropertyStrategyWrapper(PropertySetStrategy original) {
		this.original = original;
	}

	@Override
	public void setProperty(Object o, String string, Object o1)
			throws JSONException {
		try {
			original.setProperty(o, string, o1);

		} catch (Exception ex) {
		}

	}

}

class JsonDateValueProcessor implements JsonValueProcessor {     
    
    private String format ="yyyy-MM-dd";     
         
    public Object processArrayValue(Object value, JsonConfig config) {     
        return process(value);     
    }     
    
    public Object processObjectValue(String key, Object value, JsonConfig config) {     
        return process(value);     
    }     
         
    private Object process(Object value){     
             
        if(value instanceof Date){     
            SimpleDateFormat sdf = new SimpleDateFormat(format);     
            return sdf.format(value);     
        }     
        return value == null ? "" : value.toString();     
    }     
}  